/**
 * 智慧社区管理系统 - 常量定义
 * @author Wu.Liang
 * @date 2024-01-01
 */

// 项目配置
export const APP_CONFIG = {
    // 项目名称
    APP_NAME: '智慧小区管理系统',
    // 版本号
    VERSION: '1.0.0',
    // 主题色
    PRIMARY_COLOR: '#2d8cf0',
    // 品牌蓝色
    BRAND_BLUE: '#2d8cf0',
    // 成功绿色
    SUCCESS_COLOR: '#19be6b',
    // 警告橙色
    WARNING_COLOR: '#ff9900',
    // 错误红色
    ERROR_COLOR: '#ed4014'
} as const

// API相关常量
export const API_CONFIG = {
    // 请求超时时间（毫秒）
    TIMEOUT: 10000,
    // Token在localStorage中的key
    TOKEN_KEY: 'smart_community_token',
    // 用户信息在localStorage中的key
    USER_INFO_KEY: 'smart_community_user_info',
    // 语言设置key
    LOCALE_KEY: 'smart_community_locale',
    // 主题设置key
    THEME_KEY: 'smart_community_theme'
} as const

// 分页配置
export const PAGINATION_CONFIG = {
    // 默认页大小
    DEFAULT_PAGE_SIZE: 10,
    // 页大小选项
    PAGE_SIZE_OPTIONS: [10, 20, 50, 100],
    // 最大页大小
    MAX_PAGE_SIZE: 100
} as const

// 上传配置
export const UPLOAD_CONFIG = {
    // 图片类型
    IMAGE_TYPES: ['image/jpeg', 'image/jpg', 'image/png', 'image/gif'],
    // 文件类型
    FILE_TYPES: ['application/pdf', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'],
    // 图片最大大小（字节）
    MAX_IMAGE_SIZE: 5 * 1024 * 1024, // 5MB
    // 文件最大大小（字节）
    MAX_FILE_SIZE: 10 * 1024 * 1024 // 10MB
} as const

// 正则表达式
export const REGEX_PATTERNS = {
    // 手机号
    PHONE: /^1[3-9]\d{9}$/,
    // 邮箱
    EMAIL: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/,
    // 身份证号
    ID_CARD: /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/,
    // 密码（8-16位，包含字母和数字）
    PASSWORD: /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d@$!%*#?&]{8,16}$/,
    // 用户名（4-16位字母数字下划线）
    USERNAME: /^[a-zA-Z0-9_]{4,16}$/
} as const

// 日期格式
export const DATE_FORMATS = {
    DATE: 'YYYY-MM-DD',
    TIME: 'HH:mm:ss',
    DATETIME: 'YYYY-MM-DD HH:mm:ss',
    DATETIME_MINUTE: 'YYYY-MM-DD HH:mm',
    MONTH: 'YYYY-MM',
    YEAR: 'YYYY'
} as const

// 状态码
export const STATUS_CODES = {
    SUCCESS: 200,
    CREATED: 201,
    BAD_REQUEST: 400,
    UNAUTHORIZED: 401,
    FORBIDDEN: 403,
    NOT_FOUND: 404,
    INTERNAL_SERVER_ERROR: 500
} as const

// 用户角色（已迁移到 enums.ts，建议使用新的枚举）
export const USER_ROLES = {
    SUPER_ADMIN: 'SUPER_ADMIN',
    ADMIN: 'admin',
    COMMUNITY_MANAGER: 'COMMUNITY_ADMIN',
    PROPERTY_MANAGER: 'PROPERTY_ADMIN',
    SECURITY_GUARD: 'SECURITY_GUARD',
    RESIDENT: 'USER'
} as const

// 社区模块
export const MODULES = {
    USER: 'user',
    PROPERTY: 'property',
    PARKING: 'parking',
    SECURITY: 'security',
    EMERGENCY: 'emergency',
    COMMUNITY: 'community',
    NOTIFICATION: 'notification',
    PAYMENT: 'payment',
    APPOINTMENT: 'appointment',
    DEVICE: 'device',
    COMPLAINT: 'complaint'
} as const

/**
 * 系统常量定义
 * @author Wu.Liang
 * @date 2024-12-21
 * @description 定义系统中使用的各种常量
 */



/**
 * 用户类型常量
 */
export const USER_TYPE = {
  ADMIN: 'admin',           // 管理员
  SUPER_ADMIN: 'super_admin', // 超级管理员
  RESIDENT: 'resident',     // 居民
  STAFF: 'staff'           // 工作人员
} as const

/**
 * 数据权限范围常量
 */
export const DATA_SCOPE = {
  ALL: 'ALL',     // 全部数据
  NONE: 'NONE',   // 无数据权限
  SELF: 'SELF'    // 仅本人数据
} as const

/**
 * 分页默认配置
 */
export const PAGINATION = {
  DEFAULT_PAGE_SIZE: 10,
  PAGE_SIZE_OPTIONS: [10, 20, 50, 100],
  DEFAULT_CURRENT: 1
} as const

/**
 * 表格配置
 */
export const TABLE = {
  DEFAULT_ROW_KEY: 'id',
  DEFAULT_STRIPE: true,
  DEFAULT_BORDER: true
} as const

/**
 * 表单验证规则
 */
export const VALIDATION_RULES = {
  USERNAME: {
    required: true,
    min: 2,
    max: 20,
    message: '用户名长度在 2 到 20 个字符',
    trigger: 'blur'
  },
  PASSWORD: {
    required: true,
    min: 5,
    max: 20,
    message: '密码长度在 5 到 20 个字符',
    trigger: 'blur'
  },
  EMAIL: {
    type: 'email',
    message: '请输入正确的邮箱地址',
    trigger: 'blur'
  },
  PHONE: {
    pattern: /^1[3-9]\d{9}$/,
    message: '请输入正确的手机号码',
    trigger: 'blur'
  },
  CAPTCHA: {
    required: true,
    pattern: /^\d{4}$/,
    message: '请输入4位数字验证码',
    trigger: 'blur'
  }
} as const

/**
 * 系统配置
 */
export const SYSTEM_CONFIG = {
  APP_NAME: '智慧小区管理系统',
  APP_VERSION: '1.0.0',
  COPYRIGHT: '© 2024 智慧小区管理系统. All rights reserved.',
  DEFAULT_AVATAR: '/avatar/default.png',
  LOGO_URL: '/logo.png'
} as const

/**
 * 路由常量
 */
export const ROUTES = {
  LOGIN: '/login',
  DASHBOARD: '/dashboard',
  HOME: '/',
  NOT_FOUND: '/404',
  FORBIDDEN: '/403'
} as const

/**
 * 权限常量
 */
export const PERMISSIONS = {
  // 用户管理权限
  USER_VIEW: 'user:view',
  USER_CREATE: 'user:create',
  USER_UPDATE: 'user:update',
  USER_DELETE: 'user:delete',
  
  // 角色管理权限
  ROLE_VIEW: 'role:view',
  ROLE_CREATE: 'role:create',
  ROLE_UPDATE: 'role:update',
  ROLE_DELETE: 'role:delete',
  
  // 权限管理权限
  PERMISSION_VIEW: 'permission:view',
  PERMISSION_CREATE: 'permission:create',
  PERMISSION_UPDATE: 'permission:update',
  PERMISSION_DELETE: 'permission:delete',
  
  // 区域管理权限
  REGION_VIEW: 'region:view',
  REGION_CREATE: 'region:create',
  REGION_UPDATE: 'region:update',
  REGION_DELETE: 'region:delete',
  
  // 社区管理权限
  COMMUNITY_VIEW: 'community:view',
  COMMUNITY_CREATE: 'community:create',
  COMMUNITY_UPDATE: 'community:update',
  COMMUNITY_DELETE: 'community:delete',
  
  // 楼栋管理权限
  BUILDING_VIEW: 'building:view',
  BUILDING_CREATE: 'building:create',
  BUILDING_UPDATE: 'building:update',
  BUILDING_DELETE: 'building:delete',
  
  // 房户管理权限
  HOUSEHOLD_VIEW: 'household:view',
  HOUSEHOLD_CREATE: 'household:create',
  HOUSEHOLD_UPDATE: 'household:update',
  HOUSEHOLD_DELETE: 'household:delete',
  
  // 物业管理权限
  PROPERTY_VIEW: 'property:view',
  PROPERTY_CREATE: 'property:create',
  PROPERTY_UPDATE: 'property:update',
  PROPERTY_DELETE: 'property:delete',
  
  // 系统管理权限
  SYSTEM_VIEW: 'system:view',
  SYSTEM_CONFIG: 'system:config',
  SYSTEM_LOG: 'system:log',
  SYSTEM_ADMIN: 'system:admin'
} as const

/**
 * 角色常量（已迁移到 enums.ts，建议使用新的枚举）
 */
export const ROLES = {
  SUPER_ADMIN: 'SUPER_ADMIN',
  ADMIN: 'admin',
  STAFF: 'PROPERTY_STAFF',
  RESIDENT: 'USER'
} as const

/**
 * 消息类型常量
 */
export const MESSAGE_TYPE = {
  SUCCESS: 'success',
  WARNING: 'warning',
  INFO: 'info',
  ERROR: 'error'
} as const

/**
 * 操作类型常量
 */
export const OPERATION_TYPE = {
  CREATE: 'create',
  UPDATE: 'update',
  DELETE: 'delete',
  VIEW: 'view',
  EXPORT: 'export',
  IMPORT: 'import'
} as const

/**
 * 缓存键常量
 */
export const CACHE_KEYS = {
  TOKEN: 'token',
  REFRESH_TOKEN: 'refreshToken',
  USER_INFO: 'userInfo',
  REMEMBERED_USERNAME: 'rememberedUsername',
  THEME: 'theme',
  LANGUAGE: 'language'
} as const

/**
 * 常量统一导出文件
 * 统一导出所有常量，方便其他模块导入使用
 * 
 * @author Wu.Liang
 * @date 2025-01-30
 */

// ==================== 消息常量导出 ====================
export {
  MESSAGES,
  USER_MESSAGES,
  CACHE_MESSAGES,
  CRYPTO_MESSAGES,
  FILE_MESSAGES,
  PAYMENT_MESSAGES,
  PROPERTY_MESSAGES,
  VALIDATION_MESSAGES,
  getMessage,
  getUserMessage,
  getCacheMessage,
  getCryptoMessage,
  getFileMessage,
  getPaymentMessage,
  getPropertyMessage,
  getValidationMessage
} from './messages'

// ==================== 角色常量导出 ====================
export {
  ROLE_CODES,
  ROLE_NAMES,
  PERMISSION_PREFIXES,
  ROLE_GROUPS,
  getRoleName,
  isAdminRole,
  isSuperAdminRole,
  isRegionManagementRole,
  isPropertyManagementRole,
  isSecurityManagementRole,
  isManagementRole,
  isRoleInGroup,
  getRolePermissionPrefix
} from './roles'

// ==================== 状态常量导出 ====================
export {
  STATUS,
  USER_STATUS,
  GENDER_STATUS,
  PRIORITY_STATUS,
  URGENCY_STATUS,
  getStatus,
  getCommonStatus,
  getUserStatus,
  getGenderStatus,
  getPriorityStatus,
  getUrgencyStatus
} from './status'

// ==================== 权限常量导出 ====================
export {
  PERMISSION_CODES,
  PERMISSION_GROUPS,
  PERMISSION_ACTIONS,
  PERMISSION_TYPES,
  getPermissionCode,
  parsePermissionCode,
  isValidPermissionCode,
  getPermissionDescription
} from './permissions'

// ==================== 业务常量导出 ====================
export {
  BUSINESS_CONFIG,
  BUSINESS_RULES,
  BUSINESS_STATUS,
  BUSINESS_TYPES,
  BUSINESS_SETTINGS,
  getBusinessConfig,
  getBusinessRule,
  getBusinessStatus,
  getBusinessType,
  getBusinessSetting,
  isValidPhone,
  isValidEmail,
  isValidIdCard,
  isValidPassword,
  isValidUsername
} from './business'

// ==================== 工具函数 ====================

/**
 * 获取常量值
 * @param category 常量类别
 * @param key 常量键
 * @returns 常量值
 */
export function getConstant(category: string, key: string): any {
  // 这里可以根据需要实现动态获取常量的逻辑
  // 目前先返回null，后续可以根据实际需求扩展
  return null
}

/**
 * 检查常量是否存在
 * @param category 常量类别
 * @param key 常量键
 * @returns 是否存在
 */
export function hasConstant(category: string, key: string): boolean {
  return getConstant(category, key) !== null
}

/**
 * 获取所有常量类别
 * @returns 常量类别列表
 */
export function getConstantCategories(): string[] {
  return [
    'messages',
    'userMessages',
    'cacheMessages',
    'cryptoMessages',
    'fileMessages',
    'paymentMessages',
    'propertyMessages',
    'validationMessages',
    'roleCodes',
    'roleNames',
    'permissionPrefixes',
    'roleGroups',
    'status',
    'userStatus',
    'genderStatus',
    'priorityStatus',
    'urgencyStatus',
    'permissionCodes',
    'permissionGroups',
    'permissionActions',
    'permissionTypes',
    'businessConfig',
    'businessRules',
    'businessStatus',
    'businessTypes',
    'businessSettings'
  ]
}

/**
 * 获取指定类别的所有常量键
 * @param category 常量类别
 * @returns 常量键列表
 */
export function getConstantKeys(category: string): string[] {
  const constant = getConstant(category, '')
  if (constant && typeof constant === 'object') {
    return Object.keys(constant)
  }
  return []
}
